/**
 * 
 */
package id.web.faisalabdillah.xtimesheet.web.ws;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import id.web.faisalabdillah.xtimesheet.domain.HolidaysEvent;
import id.web.faisalabdillah.xtimesheet.services.IHolidaysService;
import id.web.faisalabdillah.xtimesheet.web.dto.DTOJsonResult;
import id.web.faisalabdillah.xtimesheet.web.model.HolidaysModel;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author faitz
 *
 */

@Controller
@RequestMapping("/service/holiday")
public class HolidaysServiceCtrl {

	@Autowired
	private IHolidaysService holidayService;
	
	@RequestMapping("/tanggal/{date}")
	public @ResponseBody DTOJsonResult<HolidaysEvent> getByTanggal(@PathVariable String date){
		
		DTOJsonResult<HolidaysEvent> data=new DTOJsonResult<HolidaysEvent>();
		try {
			DateTimeFormatter dtf=DateTimeFormat.forPattern("dd-MM-yyyy");
			DateTime jTgl=dtf.parseDateTime(date);
			System.out.println(jTgl.getMillis());
			data.setData(holidayService.findByDate(jTgl.toDate()));
			data.setSuccess(true);
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
			e.printStackTrace();
		}
		return data;
	}
	
	@RequestMapping("/all")
	public @ResponseBody DTOJsonResult<List<HolidaysEvent>> listAll(){
		DTOJsonResult<List<HolidaysEvent>> data=new DTOJsonResult<List<HolidaysEvent>>();
		try {
			data.setData(holidayService.findAll());
			data.setResultSize(holidayService.sizeAll());
			data.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping("/all/{first}/{max}")
	public @ResponseBody DTOJsonResult<List<HolidaysEvent>> listAllPaged(@PathVariable int first,@PathVariable int max){
		DTOJsonResult<List<HolidaysEvent>> data=new DTOJsonResult<List<HolidaysEvent>>();
		try {
			data.setData(holidayService.findAllPaged(first, max));
			data.setResultSize(holidayService.sizeAll());
			data.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	
	@RequestMapping("/btw/{before}/{after}/{first}/{max}")
	public @ResponseBody DTOJsonResult<List<HolidaysEvent>> findByDateBetween(@PathVariable int first,@PathVariable int max,@PathVariable String before,@PathVariable String after){
		DTOJsonResult<List<HolidaysEvent>> data=new DTOJsonResult<List<HolidaysEvent>>();
		DateFormat df=new SimpleDateFormat("dd-MM-yyyy",new Locale("in"));
		try {
			data.setData(holidayService.findBetweenDatePaged(df.parse(before), df.parse(after), first, max));
			data.setResultSize(holidayService.sizeBetweenDate(df.parse(before), df.parse(after)));
			data.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	
	@RequestMapping("/desc")
	public @ResponseBody DTOJsonResult<List<HolidaysEvent>> findByDesc(@PathVariable int first,@PathVariable int max,@RequestParam String query){
		DTOJsonResult<List<HolidaysEvent>> data=new DTOJsonResult<List<HolidaysEvent>>();
		try {
			data.setData(holidayService.findByDescPaged(query, first, max));
			data.setResultSize(holidayService.sizeByParams(query));
			data.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping("/delete/{tanggal}")
	public @ResponseBody DTOJsonResult<Object> delete(@PathVariable Long tanggal){
		DTOJsonResult<Object> data=new DTOJsonResult<Object>(); 
		try {
			holidayService.delete(tanggal);
			data.setSuccess(true);
			data.setResultSize(0);
		} catch (Exception e) {
			e.printStackTrace();
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
	return data;	
	}
	
	
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public @ResponseBody DTOJsonResult<Object> insert(HolidaysModel holidaysModel,BindingResult result){
		DTOJsonResult<Object> data=new DTOJsonResult<Object>();
		try {
			if(result.hasErrors()){
				String error="";
				for (ObjectError errors : result.getAllErrors()) {
					error+=errors.getDefaultMessage()+"\n";
				}
				throw new Exception(error);
			}
			HolidaysEvent hf=new HolidaysEvent();
			hf.setTanggal(holidaysModel.getTanggal());
			hf.setDescription(holidaysModel.getDescription());
			System.out.println(holidaysModel.getTanggal().getTime());
			holidayService.insert(hf);
			data.setSuccess(true);
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody DTOJsonResult<Object> update(HolidaysModel holidaysModel,BindingResult result){
		DTOJsonResult<Object> data=new DTOJsonResult<Object>();
		try {
			if(result.hasErrors()){
				String error="";
				for (ObjectError errors : result.getAllErrors()) {
					error+=errors.getDefaultMessage()+"\n";
				}
				throw new Exception(error);
			}
			HolidaysEvent hf=new HolidaysEvent();
			hf.setTanggal(holidaysModel.getTanggal());
			hf.setDescription(holidaysModel.getDescription());
			holidayService.update(hf);
			data.setSuccess(true);
		} catch (Exception e) {
			data.setSuccess(false);
			data.setError(e.getMessage());
		}
		return data;
	}
}
